package mwn.droidbus.elements;

import java.util.ArrayList;

import android.util.Log;

import com.google.android.maps.GeoPoint;

/**
 * The Class Bus.
 * Represents a bus in the real world. 
 */
public class Bus {

	/** The ID. 
	 * Each bus has a unique id as there may be multiple busses with the same routeID at one time
	 */
	private String ID;
	
	/** The route id.
	 *  E.g. 861 or 840F
	 */
	private String routeID;
	
	/** The coordinates. */
	private GeoPoint coordinates;
	
	private GeoPoint prevCoordinates;
	
	/** The bearing. */
	private double bearing;
	
	/** The origin name. */
	private String originName; 
	
	/** The destination name. */
	private String destinationName;
	
	/** The via names. */
	private ArrayList<String> viaNames;
	
	/**
	 * Instantiates a new bus.
	 *
	 * @param ID the iD
	 * @param routeID the route id
	 * @param latitude the latitude
	 * @param longitude the longitude
	 * @param bearing the bearing
	 */
	public Bus(String ID, String routeID, double latitude, double longitude, double bearing) {
		this.ID = ID;
		this.routeID = routeID;
		this.coordinates = this.degreesToGeoPoint(latitude, longitude);
		this.bearing = bearing;
	}
	
	public Bus(String ID, String routeID, int latitude, int longitude, double bearing) {
		this.ID = ID;
		this.routeID = routeID;
		this.coordinates = new GeoPoint(latitude,longitude);
		this.bearing = bearing;
	}
	
	public GeoPoint degreesToGeoPoint(double lat, double lon) {
		Double latitude = lat * 1000000;
		Double longitude = lon * 1000000;

		int glat = latitude.intValue();
		int glon = longitude.intValue();

		return new GeoPoint(glat,glon);
	}
	public Bus(Bus b) { //copy constructor
		this.ID = b.ID;
		this.routeID = b.getRouteID();
		this.coordinates = b.getCoordinates();
		this.bearing = b.getBearing();
	}
	
	/**
	 * Instantiates a new bus.
	 *
	 * @param ID the iD
	 * @param routeID the route id
	 */
	public Bus(String ID, String routeID) {
		this(ID, routeID, 0, 0, 0);
	}
	
	public void updateLocation(int latitude, int longitude) {
		prevCoordinates = coordinates;
		coordinates = new GeoPoint(latitude, longitude);
	}
	
	public void updateLocation(GeoPoint g) {
		prevCoordinates = coordinates;
		coordinates = g;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "Bus: " + ID + " on route: " + routeID + " at location: [" + coordinates.getLatitudeE6() + "," + coordinates.getLongitudeE6() + "] going from: " + originName + " to: " + destinationName + " via: " + viaNames;
	}
	
	/**
	 * Gets the iD.
	 *
	 * @return the iD
	 */
	public String getID() {
		return ID;
	}
	
	/**
	 * Sets the iD.
	 *
	 * @param iD the new iD
	 */
	public void setID(String iD) {
		ID = iD;
	}
	
	/**
	 * Gets the route id.
	 *
	 * @return the route id
	 */
	public String getRouteID() {
		return routeID;
	}
	
	/**
	 * Sets the route id.
	 *
	 * @param routeID the new route id
	 */
	public void setRouteID(String routeID) {
		this.routeID = routeID;
	}
	
	/**
	 * Gets the coordinates.
	 *
	 * @return the coordinates
	 */
	public GeoPoint getCoordinates() {
		return coordinates;
	}
	
	/**
	 * Sets the coordinates.
	 *
	 * @param coordinates the new coordinates
	 */
	public void setCoordinates(GeoPoint coordinates) {
		this.coordinates = coordinates;
	}
	
	/**
	 * Gets the bearing.
	 *
	 * @return the bearing
	 */
	public double getBearing() {
		return bearing;
	}
	
	/**
	 * Sets the bearing.
	 *
	 * @param bearing the new bearing
	 */
	public void setBearing(double bearing) {
		this.bearing = bearing;
	}
	
	/**
	 * Gets the origin name.
	 *
	 * @return the origin name
	 */
	public String getOriginName() {
		return originName;
	}
	
	/**
	 * Sets the origin name.
	 *
	 * @param originName the new origin name
	 */
	public void setOriginName(String originName) {
		this.originName = originName;
	}
	
	/**
	 * Gets the destination name.
	 *
	 * @return the destination name
	 */
	public String getDestinationName() {
		return destinationName;
	}
	
	/**
	 * Sets the destination name.
	 *
	 * @param destinationName the new destination name
	 */
	public void setDestinationName(String destinationName) {
		this.destinationName = destinationName;
	}
	
	/**
	 * Gets the via names.
	 *
	 * @return the via names
	 */
	public ArrayList<String> getViaNames() {
		return viaNames;
	}
	
	/**
	 * Sets the via names.
	 *
	 * @param viaNames the new via names
	 */
	public void setViaNames(ArrayList<String> viaNames) {
		this.viaNames = viaNames;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((ID == null) ? 0 : ID.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Bus other = (Bus) obj;
		if (ID == null) {
			if (other.ID != null)
				return false;
		} else if (!ID.equals(other.ID))
			return false;
		return true;
	}
	
	
}
